package com.xzc.datastruc.ujava.c07._1_introduction;

import org.junit.Test;

public final class RobustPrintInt {
    private static final String DIGIT_TABLE = "0123456789abcdef";
    private static final int MAX_BASE = DIGIT_TABLE.length();

    /**
     * @desc print n in any base, recursively
     * @preconditioin n>=0, 2<=base<=MAX_BASE
     */
    private static void printIntRec(long n, int base) {
        if (n >= base)
            printIntRec(n / base, base);
        System.out.println(DIGIT_TABLE.charAt((int) n % base));
    }

    /**
     * @desc driver routine
     */
    public static void printInt(long n, int base) {
        if (base <= 1 || base > MAX_BASE) {
            System.err.println("Cannot print in base " + base);
        } else {
            if (n < 0) {
                n = -n;
                System.out.println("-");
            }
            printIntRec(n, base);
        }
    }

    @Test
    public void test() {
        printInt(16, 16);
    }
}
